require 'rubygems'
require 'parseexcel'
require 'active_record'

mysql_database = '1kgorg_dev'
mysql_username = 'root'
mysql_password = ''

ActiveRecord::Base.establish_connection(
  :adapter => 'mysql',
  :host => 'localhost',
  :database => mysql_database,
  :username => mysql_username,
  :password => mysql_password
)

class School < ActiveRecord::Base
end

workbook = Spreadsheet::ParseExcel.parse(ARGV[0])

areas = %w(2231 2233 2894 2910)
count = 0

0.upto(areas.size-1) do |index|
  ws = workbook.worksheet(index)

  ws.each(2) do |row|
    count += 1
    puts "校名: " + title = row.at(5).to_s('utf-8')
    puts "地址: " + location =  row.at(0).to_s('utf-8')
    puts "邮编: " + zipcode = row.at(3).to_s('utf-8')
    puts "校长: " + schoolmaster = row.at(4).to_s('utf-8')
    puts "学校电话: " + school_telephone = row.at(2).to_s('utf-8')
  
    school = School.new
    school.submitor_id = 1
    school.info_source = "dizhen.1kg.org"
    school.area_id = areas[index]
    school.title = title
    school.address = location
    school.zipcode = zipcode
    school.master = schoolmaster
    school.telephone = school_telephone
    school.is_meta = true
    school.edu_type = 2
    school.save
  end
end

puts count